package com.yihu.cms.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.yihu.article.beans.Article;
import com.yihu.beans.ResultVO;
import com.yihu.beans.dic.DatadicQuery;
import com.yihu.beans.dic.Resource;
import com.yihu.cms.service.ArticleService;
import com.yihu.cms.service.DataDictionaryService;
import com.yihu.cms.util.CommonUtils;

@Controller
public class DataDicResourceController {
private final Logger logger = LoggerFactory.getLogger(DataDicResourceController.class); 
	DataDictionaryService service = new DataDictionaryService();
	
	@RequestMapping(value = "/datadic", method = RequestMethod.POST)
	public @ResponseBody Object add(@RequestBody Resource resource) {
			logger.debug("/datadic/resource.add: "+resource);
		ResultVO result = service.addDataResource(resource);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/datadic/{id}", method = RequestMethod.DELETE)
	public @ResponseBody Object delete(@PathVariable("id") String id) {
		logger.debug("/datadic/resource.delete: "+id);
		Resource resource = new Resource();
		resource.setId(id);
		ResultVO result = service.deleteDataResource(resource);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/datadic/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getResource(@PathVariable("id") String id) {
		logger.debug("getResource: "+id);
		Resource resource = new Resource();
		resource.setId(id);
		Object o = service.getDataResource(resource);
		return JSON.parse(JSON.toJSONString(o));
	}
	
	
	@RequestMapping(value = "/datadic/{id}", method = RequestMethod.PUT)
	public @ResponseBody Object update(@RequestBody Resource resource,@PathVariable("id") String id) {
		resource.setId(id);
		logger.debug("/datadic/resource.update: "+resource);
		ResultVO result = service.updateDataResource(resource);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/datadic/list", method = RequestMethod.GET)
	public @ResponseBody Object getresourcesByParent(DatadicQuery query) {
		logger.debug("getresourcesByParent: "+query);
		Object o = service.getDataResources(query);
		return JSON.parse(JSON.toJSONString(o));
	}
	
	//获取一个parent下面所有的childrens  exp: parentPath=cms/product/category/computer,开始没有"/"
	//http://127.0.0.1:8080/cms/datadic/childrens?parentPath=cms/product/category/computer
	@RequestMapping(value = "/datadic/childrens", method = RequestMethod.GET)
	public @ResponseBody Object getChildrenData(DatadicQuery query) {
		logger.debug("getChildrenData: "+query);
		Object o = service.getChildrenByParent(query);
		return JSON.parse(JSON.toJSONString(o));
	}
	
	//获取当前分类所有的父分类导航
	@RequestMapping(value = "/datadic/parents/{id}", method = RequestMethod.GET)
	public @ResponseBody Object getParents(@PathVariable("id") String id) {
		logger.debug("getParents: "+id);
		Object o = service.getParentsByCurrentId(id);
		return JSON.parse(JSON.toJSONString(o));
	}
	
	//通过parentPath and keyName 获取对象值
	@RequestMapping(value = "/datadic/path", method = RequestMethod.GET)
	public @ResponseBody Object getDicByPath(DatadicQuery query) {
		logger.debug("getDicByPath: "+query);
		Object o = service.getDicByPath(query);
		return JSON.parse(JSON.toJSONString(o));
	}
	
	
	
		
}
